package fr.mmt.gisfizz.service;

import fr.mmt.gisfizz.entity.Workspace;
import fr.mmt.gisfizz.repository.WorkspaceRepository;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Workspace CRUD Service.<br/>
 *
 * @author Mathieu MAST
 */
@Transactional
@Service
@Slf4j
public class WorkspaceCrudServiceImpl implements WorkspaceCrudService {

    @Resource
    private WorkspaceRepository workspaceRepository;

    @Override
    public long count() {
        return workspaceRepository.count();
    }

    @Override
    public void delete(String pk) {
        workspaceRepository.delete(pk);
    }

    @Override
    public void delete(Workspace obj) {
        workspaceRepository.delete(obj);
    }

    @Override
    public Page<Workspace> search(Map<String, String> searchMap, Pageable page) {
        String search = searchMap.get("search");
        if (StringUtils.isEmpty(search)) {
            return workspaceRepository.findAll(page);
        } else {
            return workspaceRepository.search('%' + StringUtils.lowerCase(search) + '%', page);
        }
    }

    @Override
    public Workspace getByPk(String pk) {
        return workspaceRepository.findOne(pk);
    }

    @Override
    public Workspace save(Workspace obj) {
        workspaceRepository.save(obj);
        return obj;
    }
}
